
package ${package};

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ${name} {

    //static reference to itself
    private static ConnectionFactory instance = new ConnectionFactory();
    public static final String USER = "${user}";
    public static final String PASSWORD = "${password}";
	<#if "mysql" == driver >
	public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
	public static final String URL = "jdbc:mysql://localhost/${db}";
	</#if>
    <#if "postgres" == driver >
	public static final String DRIVER_CLASS = "org.postgresql.Driver";
	public static final String URL = "jdbc:postgresql://127.0.0.1:5432/${db}";
	</#if>	
    

    //private constructor
    private ConnectionFactory() {
        try {
            Class.forName(DRIVER_CLASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private Connection createConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            System.out.println("ERROR: Unable to Connect to Database.");
        }
        return connection;
    }

    public static Connection getConnection() {
        return instance.createConnection();
    }
}